package com.qiuyang.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiuyang.mapper.BlogMapper;
import com.qiuyang.pojo.Blog;
import com.qiuyang.pojo.Tag;
import com.qiuyang.pojo.Type;
import com.qiuyang.pojo.User;
import com.qiuyang.service.BlogService;
import com.qiuyang.service.TagService;
import com.qiuyang.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @Autowired BlogService blogService;



    //查询
    @RequestMapping("/findBlog")
    public String findBlog(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, Model model){
        PageHelper.startPage(pageNum,5);
        model.addAttribute("pageInfo",new PageInfo<>(blogService.findBlogs()));
        return "/admin/blogs";
    }

    //根据title 查询 模糊查询
    @RequestMapping("/findByTitle")
    public String findByTitle(@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum, Model model,String title){
        PageHelper.startPage(pageNum,5);
        model.addAttribute("pageInfo",new PageInfo<>(blogService.findByTitle(title)));
        return "admin/blogs";
    }




    //去添加
    @RequestMapping("/toInsertBlog")
    public String toinsertBlog(Model model){
        List<Type> findall = typeService.getAllType();
        List<Tag> all = tagService.findAll();
        model.addAttribute("type",findall);
        model.addAttribute("tag",all);
        return "admin/blogs-input";
    }

    //添加
    @RequestMapping("/insertBlog/{published}")
    @ResponseBody
    public String InsertBlog(@PathVariable("published") int published,Blog blog,String tagsId){
        String blogFlag = blog.getFlag().replaceAll("\\s*", "");
        String blogTitle = blog.getTitle().replaceAll("\\s*", "");
        String blogContext = blog.getContext().replaceAll("\\s*", "");
        String blogFirstPicture = blog.getFirstPicture().replaceAll("\\s*", "");
        String blogtags  = tagsId.replaceAll("\\s*", "");
        if (blogTitle.length()==0){
            return "标题不能为空！";
        }
        if (blogContext.length()==0){
            return "内容不能为空！";
        }
        if (blog.getTypeId()==null){
            return "请选择分类";
        }
        if (blogFirstPicture.length()==0){
            return "请输入首图地址";
        }
        if (blogFlag.length()==0){
            blog.setFlag("原创");
        }
        if (blogtags.length()==0){
            return "请选择标签";
        }
        blogService.insertBlog(blog);

        Map<String,Object> map = new HashMap<>();
        map.put("blogsId",blogService.findMaxId());
        map.put("tagsId",","+tagsId+",");
        blogService.insertBlogTags(map);
        return "添加成功！";
    }



    //去修改
    @RequestMapping("/toUpdateBlog/{id}")
    public String toUpdateBlog(@PathVariable Long id,Model model){
        model.addAttribute("type",typeService.getAllType());
        model.addAttribute("blogs",blogService.findBlogById(id));
        model.addAttribute("tag",tagService.findAll());
        model.addAttribute("blogtags",blogService.findAllBlogTags(id));

        return "admin/blogs-update";
    }

    //修改
    @RequestMapping("/updateBlog/{published}")
    @ResponseBody
    public String updateBlog(Blog blog,Model model,String tagsId){

        String blogFlag = blog.getFlag().replaceAll("\\s*", "");
        String blogTitle = blog.getTitle().replaceAll("\\s*", "");
        String blogContext = blog.getContext().replaceAll("\\s*", "");
        String blogFirstPicture = blog.getFirstPicture().replaceAll("\\s*", "");
        String blogtags  = tagsId.replaceAll("\\s*", "");

        if (blogTitle.length()==0){
            return "标题不能为空！";
        }
        if (blogContext.length()==0){
            return "内容不能为空！";
        }
        if (blog.getTypeId()==null){
            return "请选择分类";
        }
        if (blogFirstPicture.length()==0){
            return "请输入首图地址";
        }
        if (blogFlag.length()==0){
            blog.setFlag("原创");
        }
        if (blogtags.length()==0){
            return "请选择标签";
        }

        if (!tagsId.endsWith(",")){
            tagsId = tagsId + ",";
        }


        Map<String,Object> map = new HashMap<>();
        blogService.updateBlog(blog);
        map.put("blogsId",blog.getId());
        map.put("tagsId",tagsId);
        blogService.updateBlogTags(map);
        return "修改成功！";
    }


    //删除
    @RequestMapping("/deleteBlog/{id}")
    public String deleteById(@PathVariable Long id){
        blogService.deleteBlog(id);
        blogService.deleteBlogTag(id);
        return "redirect:/admin/findBlog";
    }





}
